package com.godofgamblers.hotelmanagementsystem.handler;

import com.godofgamblers.hotelmanagementsystem.dto.OrderMes;
import com.godofgamblers.hotelmanagementsystem.pojo.Reservation;
import com.godofgamblers.hotelmanagementsystem.pojo.Reservation_orderItem;
import com.godofgamblers.hotelmanagementsystem.service.ReservationService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Haonan
 * @date 2019/10/12 - 17:34
 */
@Controller
@Data
public class ReservationHandler {
    @Autowired
    private ReservationService reservationService;
    private List<Reservation> reservations;
    private List<Reservation> reservationsHeader;
    private List<Reservation_orderItem> reservation_orderItems;
    //用名字查询预订信息(模糊查询)
    @RequestMapping("/select_reservation_byName")
    @ResponseBody
    public Map<String, Object> select_reservation_byName(
            @RequestParam String username
    ) {
        //默认预定不为空
        String empty = "";
        // 不能将空值带入模糊查询
        if(username != "") {
            reservations = reservationService.findCurrentReservationByUsername(username);
        }
        //遍历订单找到订单下面的订单项
        for(Reservation reservation:reservations) {
            reservation.setReservation_orderItemList(reservationService.findCurrentReservationItemByOrderId(reservation.getOrder_id()));
        }
        // 如果预定为空返回这个
        if (reservations.isEmpty()) {
            empty = "empty";
        }
        Map<String,Object> map = new HashMap<>();
        map.put("empty",empty);
        map.put("result_reservations", reservations);
        return map;
    }

    //用客户手机查询预订信息(模糊查询)
    @RequestMapping("/select_reservation_byPhone")
    @ResponseBody
    public Map<String,Object> select_reservation_byPhone(
        @RequestParam String phoneNum
    ){
        //默认预定不为空
        String empty = "";
        // 不能将空值带入模糊查询
        if (phoneNum != "") {
            reservations = reservationService.findCurrentReservationByPhone(phoneNum);
        }
        //遍历订单找到订单下面的订单项
        for(Reservation reservation:reservations) {
            reservation.setReservation_orderItemList(reservationService.findCurrentReservationItemByOrderId(reservation.getOrder_id()));
        }
        // 如果预定为空返回这个
        if (reservations.isEmpty()) {
            empty = "empty";
        }
        Map<String,Object> map = new HashMap<>();
        map.put("empty",empty);
        map.put("result_reservations", reservations);
        return map;
    }

    //用订单项编号软删除订单项
    @RequestMapping("/update_reservation_item_flag")
    @ResponseBody
    public Map<String,Object> update_reservation_item_flag(
            @RequestParam String order_item_id,
            @RequestParam String phoneNum
    ) {
        String result_info = "删除失败";
        if(reservationService.updateCurrentReservationItemFlagByOrderId(order_item_id)) {
            result_info = "成功删除所选订单项";
        }
        reservations = reservationService.findCurrentReservationByPhone(phoneNum);
        //遍历订单找到订单下面的订单项
        for(Reservation reservation:reservations) {
            reservation.setReservation_orderItemList(reservationService.findCurrentReservationItemByOrderId(reservation.getOrder_id()));
            //如果订单没有订单项软删除订单
            if(reservation.getReservation_orderItemList().isEmpty()) {
                reservationService.updateCurrentReservationFlagByOrderId(reservation.getOrder_id());
            }
        }
        // 再对reservations进行一次过滤只保留flag为1的订单 或者 订单项不为空的订单
        for (int i = 0; i < reservations.size(); i ++) {
            if (reservations.get(i).getFlag() == 0 || reservations.get(i).getReservation_orderItemList().isEmpty()) {
                reservations.remove(i);
                i--; //删除把索引往前拉一位
            }
        }

        Map<String,Object> map = new HashMap<>();

        map.put("result_reservations", reservations);
        map.put("result_info",result_info);
        return map;
    }

    // 找到订单项信息传给开页面订单
    @RequestMapping("/get_order_item")
    public ModelAndView getOrderitem(
            @RequestParam int order_item_id
    ) {
        ModelAndView modelAndView = new ModelAndView();
        OrderMes orderMes = new OrderMes();
        orderMes = reservationService.findReservationItemByOrderItemId(order_item_id);
        modelAndView.addObject("OrderInfo",orderMes);
        modelAndView.setViewName("back_operate");
        return modelAndView;
    }

    // 找到订单中的未读项展示在铃铛上
    @RequestMapping("/unread_order_count")
    @ResponseBody
    public Map<String,Object> findCurrentUnreadOrderCount(

    ) throws ParseException {
        SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long minBetween = 0;

        reservationsHeader = reservationService.findUnreadComment();

/*        // 问题所在
        // 遍历订单找到订单下面的订单项
        for(Reservation reservation:reservationsHeader) {
            reservation.setReservation_orderItemList(reservationService.findCurrentReservationItemByOrderId(reservation.getOrder_id()));
        }*/

        // 找出最近的订单创建时间
        for(int i = 0; i < reservationsHeader.size(); i++ ) {
            // 订单创建的时间
            Date begin = dfs.parse(reservationsHeader.get(i).getOrder_time());
            // 当前的时间
            Date end = new Date();
            long between = (end.getTime() - begin.getTime())/1000;
            if(minBetween == 0) {
                minBetween = between;
            } else if (between < minBetween) {
                minBetween = between;
            }
        }

        // 转换成分钟
        minBetween = minBetween/60;

        Map<String,Object> map = new HashMap<>();
        map.put("header_reservations_info", reservationsHeader);
        map.put("minBetween",minBetween);
        return map;
    }

    // 根据前台传来的order_id来更新order表中的leave_message_state, 让预定变成已读 和 把订单下的订单项传到前台
    @RequestMapping("/update_order_messageState")
    @ResponseBody
    public Map<String,Object> updateOrderMessageState(
            @RequestParam int order_id
    ) {
        String result = "变更失败";
        if(reservationService.updateOrderMessageState(order_id)) {
            result = "变更成功";
        }
        Reservation reservation = reservationService.findCurrentReservationByOrderId(order_id);
        // 找到其下面的订单项
        reservation_orderItems = reservationService.findCurrentReservationItemByOrderId(reservation.getOrder_id());
        // 将订单项存入map 返回到前台
        Map<String,Object> map = new HashMap<>();
        map.put("reservation_orderItems", reservation_orderItems);
        return map;
    }

    // 变更全部order的leave_message_state为1，让全部信息变成已读
    @RequestMapping("/update_order_all_messageState")
    @ResponseBody
    public String updateOrderAllMessageState() {
        String result = "变更全部失败";
        if(reservationService.updateOrderAllMessageState()) {
            result = "变更成功";
        }
        return result;
    }

}
